﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-9-13
Description:SMS_Example三层结构数据访问层
=============================================================================
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SQLite;
using FTchina.Models;//引入模型层
namespace FTchina.DAL
{
	public static partial class SMS_ExampleService
	{
		#region 增加一条数据
		public static int AddSMS_Example(SMS_Example sMS_Example)
		{
			string sql =
			"INSERT INTO SMS_Example(SmsName,SmsContent,isdefault) "+
			"VALUES (@SmsName,@SmsContent,@isdefault)";
			
			 
				SQLiteParameter[] para = new SQLiteParameter[]
				{
						
					new SQLiteParameter("@SmsName",sMS_Example.SmsName),
						
					new SQLiteParameter("@SmsContent",sMS_Example.SmsContent),
						
					new SQLiteParameter("@isdefault",sMS_Example.Isdefault)
				};
				int newId = DBHelper.ExecuteCommand(sql, para);
				return newId;
			 
		}
		#endregion
		#region 删除数据以主键，标识列，唯一列构建的删除
			
		public static int DeleteSMS_ExampleById(long id)	
		{
			string sql="DELETE From  SMS_Example  Where id=@id";
			 SQLiteParameter[] para = new SQLiteParameter[]
				{
					new SQLiteParameter("@id", id)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}		
		
		#endregion
		#region 修改数据
		//考虑标识列与主键不一致情形
		//以标识列修改实体
		public static int ModifySMS_ExampleByid(SMS_Example sMS_Example)
		{
		string sql ="UPDATE "+"SMS_Example"+" set "+
					"SmsName=@SmsName, "+
					"SmsContent=@SmsContent, "+
					"isdefault=@isdefault "+ 
				" Where id=@id";
			 
				SQLiteParameter[] para = new SQLiteParameter[]
				{
					new SQLiteParameter("@id",sMS_Example.Id),
					new SQLiteParameter("@SmsName",sMS_Example.SmsName),
					new SQLiteParameter("@SmsContent",sMS_Example.SmsContent),
					new SQLiteParameter("@isdefault",sMS_Example.Isdefault)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}					
		#endregion
		#region 查询数据
		public static IList<SMS_Example> GetAllSMS_Examples()
		{
			string sqlAll = "SELECT * FROM SMS_Example";
			return GetSMS_ExamplesBySql(sqlAll);
		}
		//以标识列获得对象
		public static SMS_Example GetSMS_ExampleByid(object id)
		{
			string sql = "SELECT * FROM "+"SMS_Example"+" WHERE id = @id";
			
            using (SQLiteDataReader reader = DBHelper.GetReader(sql, new SQLiteParameter("@id", id)))
			{
				 
				if (reader.Read())
				{
					SMS_Example sMS_Example = new SMS_Example();
					sMS_Example.Id=Convert.ToInt32(reader["id"]);
					sMS_Example.SmsName=Convert.ToString(reader["SmsName"]);
					sMS_Example.SmsContent=Convert.ToString(reader["SmsContent"]);
					sMS_Example.Isdefault=Convert.ToInt32(reader["isdefault"]);
					reader.Close();
					
					return sMS_Example;
					}
				else
				{
					reader.Close();
					return null;
				}
			}
			 
		}
					
		//私有方法供构建好的SQL语句调用
		private static IList<SMS_Example> GetSMS_ExamplesBySql(string safeSql)
		{
			List<SMS_Example> list = new List<SMS_Example>();
			 
            using (DataTable table = DBHelper.GetDataTable(safeSql))
			{
				foreach (DataRow row in table.Rows)
				{
					SMS_Example sMS_Example = new SMS_Example();
					sMS_Example.Id=Convert.ToInt32(row["id"]);
					sMS_Example.SmsName=Convert.ToString(row["SmsName"]);
					sMS_Example.SmsContent=Convert.ToString(row["SmsContent"]);
					sMS_Example.Isdefault=Convert.ToInt32(row["isdefault"]);
					list.Add(sMS_Example);
				}
				return list;
			}
             
		}
		private static IList<SMS_Example> GetSMS_ExamplesBySql( string sql, params SQLiteParameter[] values )
		{
			List<SMS_Example> list = new List<SMS_Example>();
			 
            using (DataTable table = DBHelper.GetDataTable(sql, values))
			{
				foreach (DataRow row in table.Rows)
				{
					SMS_Example sMS_Example = new SMS_Example();
					sMS_Example.Id=Convert.ToInt32(row["id"]);
					sMS_Example.SmsName=Convert.ToString(row["SmsName"]);
					sMS_Example.SmsContent=Convert.ToString(row["SmsContent"]);
					sMS_Example.Isdefault=Convert.ToInt32(row["isdefault"]);
					list.Add(sMS_Example);
				}
				return list;
			}
            
			 
		}
		#endregion
		 
	}
}

